/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "movement_srv_proxy.h"

#include <ipc_types.h>
#include <message_parcel.h>
#include <message_option.h>

#include "imovement_callback.h"
#include "movement_common.h"

namespace OHOS {
namespace Msdp {
void MovementSrvProxy::Subscribe(const MovementDataUtils::MovementType& type, const sptr<ImovementCallback>& cb)
{
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    MOVEMENT_RETURN_IF((remote == nullptr) || (cb == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MovementSrvProxy::GetDescriptor())) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "Write descriptor failed");
        return;
    }

    MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "callback: %{public}p", cb.GetRefPtr());

    MOVEMENT_WRITE_PARCEL_NO_RET(data, Int32, type);
    MOVEMENT_WRITE_PARCEL_NO_RET(data, RemoteObject, cb->AsObject());

    int ret = remote->SendRequest(static_cast<int>(Imovement::MOVEMENT_SUBSCRIBE), data, reply, option);
    if (ret != ERR_OK) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Exit");
}

void MovementSrvProxy::UnSubscribe(const MovementDataUtils::MovementType& type, const sptr<ImovementCallback>& cb)
{
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    MOVEMENT_RETURN_IF((remote == nullptr) || (cb == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MovementSrvProxy::GetDescriptor())) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "Write descriptor failed!");
        return;
    }

    MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "callback: %{public}p", cb.GetRefPtr());

    MOVEMENT_WRITE_PARCEL_NO_RET(data, Int32, type);
    MOVEMENT_WRITE_PARCEL_NO_RET(data, RemoteObject, cb->AsObject());

    int ret = remote->SendRequest(static_cast<int>(Imovement::MOVEMENT_UNSUBSCRIBE), data, reply, option);
    if (ret != ERR_OK) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Exit");
}

MovementDataUtils::MovementData MovementSrvProxy::GetCache(const MovementDataUtils::MovementType& type)
{
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Enter");
    MovementDataUtils::MovementData movementData;
    movementData.type = MovementDataUtils::MovementType::TYPE_INVALID;
    movementData.value = MovementDataUtils::MovementValue::VALUE_INVALID;

    sptr<IRemoteObject> remote = Remote();
    MOVEMENT_RETURN_IF_WITH_RET((remote == nullptr), movementData);

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(MovementSrvProxy::GetDescriptor())) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "Write descriptor failed!");
        return movementData;
    }

    MOVEMENT_WRITE_PARCEL_WITH_RET(data, Int32, type, movementData);

    int ret = remote->SendRequest(static_cast<int>(Imovement::MOVEMENT_GETCACHE), data, reply, option);
    if (ret != ERR_OK) {
        MOVEMENT_HILOGE(MOVEMENT_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return movementData;
    }

    int32_t movementType = -1;
    int32_t movementValue = -1;
    MOVEMENT_READ_PARCEL_WITH_RET(reply, Int32, movementType, movementData);
    MOVEMENT_READ_PARCEL_WITH_RET(reply, Int32, movementValue, movementData);
    movementData.type = MovementDataUtils::MovementType(movementType);
    movementData.value = MovementDataUtils::MovementValue(movementValue);
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "type: %{public}d", movementData.type);
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "value: %{public}d", movementData.value);
    MOVEMENT_HILOGD(MOVEMENT_MODULE_INNERKIT, "Exit");
    return movementData;
}
} // Msdp
} // OHOS